using System;

namespace xiushen.Domain
{
    public class SortParameter : IEquatable<SortParameter>
    {
        public string Column { get; set; }

        public bool IsDescending { get; set; }

        public SortParameter(string column, bool isDescending)
        {
            Column = column;
            IsDescending = isDescending;
        }

        public SortParameter(string column) : this(column, false) { }

        public override string ToString()
        {
            return Column + " " + GetDirection();
        }

        private string GetDirection()
        {
            if (IsDescending)
            {
                return "DESC";
            }
            else
            {
                return "ASC";
            }
        }

        public bool Equals(SortParameter obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj.Column, Column) && obj.IsDescending.Equals(IsDescending);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(SortParameter)) return false;
            return Equals((SortParameter)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((Column != null ? Column.GetHashCode() : 0) * 397) ^ IsDescending.GetHashCode();
            }
        }

        public static bool operator ==(SortParameter left, SortParameter right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(SortParameter left, SortParameter right)
        {
            return !Equals(left, right);
        }
    }
}